﻿using fuzhu;
using LH.Model.PU;
using LH.Model.SA;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NETCore.MailKit.Core;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Ocsp;
using Quartz.Impl.Triggers;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using XXX.api.Filter;
using XXX.Bo;
using XXX.Common;
using XXX.Models;
using XXX.Models.WDFT;
using Yonyou.U8.BO;

namespace XXX.api.Controllers.WDFT
{
    /// <summary>
    /// 销售订单
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    [TypeFilter(typeof(ApiLoggingFilterFactory))]
    public class SA_SOMainController : BaseController
    {

        private readonly ILogger<SA_SOMainController> _logger;
        //private readonly IEmailService _EmailService;


        public SA_SOMainController(ILogger<SA_SOMainController> logger)
        {
            _logger = logger;
            //_EmailService = emailService;
        }



        [HttpPost]
        public RetModel2 AddVerify(JObject obj)
        {
            string requestBody = DbHelper.GetDbString(obj);
            if (requestBody == null)
            {
                return DBService.ReErrObject("入参有误，没有数据");
            }


            #region 接口调用间隔
            string strKey = ControllerContext.RouteData.Values["controller"].ToString() + Md5Encrypt.MD5(requestBody);

            if (CacheHelpCore.GetCacheValue(strKey) == null)
            {
                CacheHelpCore.SetChacheValue(strKey, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), CanShu.chkRepeat);
            }
            else
            {
                throw new Exception("接口调用间隔过短");
            }
            #endregion 接口调用间隔

            try
            {
                string cErr = "";
                string cCode = "";
                API_SaleOrder api_entity;
                try
                {
                    api_entity = JsonConvert.DeserializeObject<API_SaleOrder>(requestBody);
                    if (api_entity == null)
                    {
                        cErr = "json结构异常!空值";
                        return null;
                    }
                }
                catch (Exception e)
                {
                    throw new Exception("json结构异常！" + e.Message);
                }
                if (string.IsNullOrWhiteSpace(api_entity.cSoCode))
                {
                    cErr = "销售订单号不能为空";
                    return null;
                }
                SaleOrderVM model = new SaleOrderVM(new SaleOrder.Head(), new List<SaleOrder.Body>());

                model = model.GetAddEntity(api_entity, ref cErr);
                RetModel2 ret;
                if (model == null)
                {
                    ret = DBService.ReErrObject("入参有误" + cErr);
                }
                else
                {

                     
                    ret = model.AddVerify();
                    //model.conStr = CanShu.conStr;                   

                }
                // 将 JSON 数据保存到数据库中
                //JsonSaveService.saveLog2("销售订单新增", cCode, requestBody, ret.code, ret.message);
                //new SendEmailService(_EmailService, _logger).Send(CanShu.mail.SendCope,ret.code, CanShu.mail.ToAddress, CanShu.cZT + "销售订单新增：" + cCode, "报文:\r\n" + requestBody + "\r\n" + "结果：\r\n" + ret.message);
                
                return ret;
            }
            catch (Exception ex)
            {
                //new SendEmailService(_EmailService, _logger).Send(CanShu.mail.SendCope,500, CanShu.mail.ToAddress, CanShu.cZT + "销售订单新增,程序错误：", "报文:\r\n" + requestBody + "\r\n" + "结果：\r\n" + ex.Message);
                return DBService.ReErrObject(ex.Message);
            }
             
        }


        [HttpPost]
        public RetModel2 UNVerify(JObject obj)
        {
            string requestBody = DbHelper.GetDbString(obj);
            if (requestBody == null)
            {
                return DBService.ReErrObject("入参有误，没有数据");
            }

            try
            {
                string cErr = "";
                API_SaleOrder api_entity;
                try
                {
                    api_entity = JsonConvert.DeserializeObject<API_SaleOrder>(requestBody);
                    if (api_entity == null)
                    {
                        cErr = "json结构异常!空值";
                        return null;
                    }
                }
                catch (Exception e)
                {
                    throw new Exception("json结构异常！" + e.Message);
                }
                SaleOrderVM model = new SaleOrderVM(new SaleOrder.Head(), new List<SaleOrder.Body>());

                model = model.GetCodeEntity(api_entity, ref cErr);
                RetModel2 ret;
                if (model == null)
                {
                    ret = DBService.ReErrObject("入参有误" + cErr);
                }
                else
                {
                    ret = model.UnverifyDelete();
                }
                // 将 JSON 数据保存到数据库中
                JsonSaveService.saveLog2("销售订单弃审删除", model.head.csocode, requestBody, ret.code, ret.message);
              

                return ret;
            }
            catch (Exception ex)
            {
                //_EmailService.Send(CanShu.mail.ToAddress, CanShu.cZT + "销售订单弃审删除,程序错误：", "报文:\r\n" + requestBody + "\r\n" + "结果：\r\n" + ex.Message);
                return DBService.ReErrObject(ex.Message);
            }

        }




        [HttpPost]
        public RetModel2 Change(JObject obj)
        {
            string requestBody = DbHelper.GetDbString(obj);
            if (requestBody == null)
            {
                return DBService.ReErrObject("入参有误，没有数据");
            }


            #region 接口调用间隔
            string strKey = ControllerContext.RouteData.Values["controller"].ToString() + Md5Encrypt.MD5(requestBody);

            if (CacheHelpCore.GetCacheValue(strKey) == null)
            {
                CacheHelpCore.SetChacheValue(strKey, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), CanShu.chkRepeat);
            }
            else
            {
                throw new Exception("接口调用间隔过短");
            }
            #endregion 接口调用间隔

            try
            {
                string cErr = "";
                string cCode = "";
                API_POMain api_entity;
                try
                {
                    api_entity = JsonConvert.DeserializeObject<API_POMain>(requestBody);
                    if (api_entity == null)
                    {
                        cErr = "json结构异常!空值";
                        return null;
                    }
                }
                catch (Exception e)
                {
                    throw new Exception("json结构异常！" + e.Message);
                }
                if (string.IsNullOrWhiteSpace(api_entity.cCode))
                {
                    cErr = "销售订单号不能为空";
                    return null;
                }
                PuPOMainVM model = new PuPOMainVM(new PurchaseOrder.Head(), new List<PurchaseOrder.Body>());

                model = model.GetAddEntity(api_entity, ref cErr, ref cCode);
                RetModel2 ret;
                if (model == null)
                {
                    ret = DBService.ReErrObject("入参有误" + cErr);
                }
                else
                { 
                    ret = model.UnverifyDelete();       
                }
               
                return ret;
            }
            catch (Exception ex)
            {
                //new SendEmailService(_EmailService, _logger).Send(CanShu.mail.SendCope,500, CanShu.mail.ToAddress, CanShu.cZT + "销售订单新增,程序错误：", "报文:\r\n" + requestBody + "\r\n" + "结果：\r\n" + ex.Message);
                return DBService.ReErrObject(ex.Message);
            }

        }



    }
}
